package master;

/**
 * @author chenyw
 * @date 2022/7/25 12:26
 */
public class M415addString {
    public String addStrings(String num1, String num2) {
        StringBuffer sb = new StringBuffer();
        int i = num1.length() - 1;
        int j = num2.length() - 1;
        //保存进位
        int a = 0;
        while (i >= 0 && j >= 0) {
            int tmp = num1.charAt(i) - '0' + num2.charAt(j) - '0';
            tmp += a;
            sb.insert(0, tmp % 10);
            a = tmp / 10;
            i--;
            j--;
        }
        while (i >= 0) {
            int tmp = num1.charAt(i) - '0' + a;
            sb.insert(0, tmp % 10);
            a = tmp / 10;
            i--;
        }
        while (j >= 0) {
            int tmp = num2.charAt(j) - '0' + a;
            sb.insert(0, tmp % 10);
            a = tmp / 10;
            j--;
        }
        if (a != 0) {
            sb.insert(0, a);
        }

        return sb.toString();
    }
}
